var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/baseComponent/datepicker.tsx
var datepicker_exports = {};
__export(datepicker_exports, {
  DatePicker: () => DatePicker,
  DateTimePicker: () => DateTimePicker,
  RangePicker: () => RangePicker,
  TimePicker: () => TimePicker
});
module.exports = __toCommonJS(datepicker_exports);
var import_antd = require("antd");
var import_zh_CN = __toESM(require("antd/es/date-picker/locale/zh_CN"));
var import_dayjs = __toESM(require("dayjs"));
var import_zh_cn = require("dayjs/locale/zh-cn");
var import_util = require("../util");
import_antd.TimePicker.displayName = "TimePicker";
var pickerFormat = {
  date: "YYYY-MM-DD",
  year: "YYYY",
  quarter: "YYYY-[Q]Q",
  month: "YYYY-MM",
  week: "YYYY-wo"
};
function convertToDate(value, format) {
  if (format === pickerFormat.quarter && value.indexOf("-Q") > 0) {
    const parts = value.split("-Q");
    const year = parseInt(parts[0], 10);
    const quarter = parseInt(parts[1], 10);
    const month = quarter * 3 - 2;
    return (0, import_dayjs.default)(new Date(year, month, 1));
  }
  return (0, import_dayjs.default)(value, format);
}
var getFormat = (props, defaultFormat, type) => {
  const showTime = props.showTime ?? type === "datetime";
  return props.format || pickerFormat[props.picker] || (showTime ? "YYYY-MM-DD " + (showTime.format || "HH:mm:ss") : "") || defaultFormat;
};
function getDatePicker(Comp, type = "", dFormat = "YYYY-MM-DD") {
  return class extends import_util.IUPComponent {
    constructor(props) {
      super(props);
      this.onDatePickerChange = (value) => {
        if (this.props.hasOwnProperty("value")) {
          this.state.value = this.getDayJs(value);
          this.props.onChange && this.props.onChange(this.getValue());
        } else {
          this.setValue(value);
        }
        this.innerNotify([value], "onChange").then();
      };
      this.onOpenChange = (open) => {
        var _a, _b, _c, _d, _e;
        if (!this.props.hasOwnProperty("open")) {
          this.setState({ open });
        }
        (_b = (_a = this.props).onOpenChange) == null ? void 0 : _b.call(_a, open);
        const targetEl = (_c = window.event) == null ? void 0 : _c.target;
        if (!open && !this.getShowTime() && targetEl) {
          if (((_e = (_d = targetEl == null ? void 0 : targetEl.className) == null ? void 0 : _d.indexOf) == null ? void 0 : _e.call(_d, "ant-picker-")) === 0) {
            document.body.style.pointerEvents = "none";
            setTimeout(() => {
              document.body.style.pointerEvents = "";
            }, 250);
          }
        }
      };
      const defaultFormat = getFormat(props, dFormat, type);
      this.state = {
        props,
        open: props.defaultOpen || props.open,
        defaultFormat,
        value: this.getDayJs(props.value ?? props.defaultValue, defaultFormat)
      };
    }
    static getDerivedStateFromProps(nextProps, prevState) {
      const { value, format, picker, showTime, open } = nextProps;
      const newState = {};
      if (open !== prevState.props.open) {
        newState.open = open;
        newState.props = nextProps;
      }
      if (value !== prevState.props.value) {
        newState.value = value;
        newState.props = nextProps;
      }
      if (format !== prevState.props.format || picker !== prevState.props.picker || showTime !== prevState.props.showTime) {
        newState.defaultFormat = getFormat(nextProps, dFormat, type);
        newState.props = nextProps;
      }
      return newState.props ? newState : null;
    }
    getShowTime() {
      return this.props.showTime ?? type === "datetime";
    }
    setValue(value, cb) {
      if (this.state.value === value) {
        return;
      }
      value = this.getDayJs(value);
      if (value && (import_util.iup.isArray(value) ? value.some((v) => !v.isValid()) : !value.isValid())) {
        return;
      }
      const newValue = value || null;
      this.setState({ value: newValue }, () => {
        this.props.onChange && this.props.onChange(this.getValue());
        cb && cb(newValue);
      });
    }
    getValue() {
      const value = this.getDayJs(this.state.value);
      if (!value) {
        return null;
      }
      return type === "range" ? value.map((val) => val ? val.format(this.state.defaultFormat) : null) : value.format(this.state.defaultFormat);
    }
    getRawValue() {
      return this.getDayJs(this.state.value || null);
    }
    isOpened() {
      return !!this.state.open;
    }
    getDayJs(value, defaultFormat) {
      if (import_util.iup.isArray(value)) {
        return value.map((v) => this.getDayJs(v));
      }
      if (import_util.iup.isDate(value)) {
        return (0, import_dayjs.default)(value);
      }
      return !value || import_dayjs.default.isDayjs(value) ? value : convertToDate(value, defaultFormat || this.state.defaultFormat);
    }
    render() {
      const { value, onChange, onOpenChange, observer, ...dataPickerProps } = this.props;
      if (dataPickerProps.disabled) {
        dataPickerProps.allowClear = false;
        dataPickerProps.suffixIcon = null;
        dataPickerProps.placeholder = "";
        dataPickerProps.inputReadOnly = true;
      } else {
        dataPickerProps.allowClear = dataPickerProps.allowClear ?? true;
        if (type === "range" && import_util.iup.isString(dataPickerProps.placeholder)) {
          dataPickerProps.placeholder = [dataPickerProps.placeholder, dataPickerProps.placeholder];
        }
      }
      return /* @__PURE__ */ React.createElement(
        Comp,
        {
          ref: this.outRef,
          style: { width: "100%" },
          locale: import_zh_CN.default,
          ...dataPickerProps,
          open: this.state.open,
          showTime: this.getShowTime(),
          value: this.getDayJs(this.state.value),
          onOpenChange: this.onOpenChange,
          onChange: this.onDatePickerChange
        }
      );
    }
  };
}
var DatePicker = (0, import_util.compHoc)(getDatePicker(import_antd.DatePicker));
var DateTimePicker = (0, import_util.compHoc)(
  getDatePicker(import_antd.DatePicker, "datetime")
);
var RangePicker = (0, import_util.compHoc)(getDatePicker(import_antd.DatePicker.RangePicker, "range"));
var TimePicker = (0, import_util.compHoc)(getDatePicker(import_antd.TimePicker, "", "HH:mm:ss"));
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  DatePicker,
  DateTimePicker,
  RangePicker,
  TimePicker
});
